/**
 * CustomerDataPanel.java
 * @author pkendzo
 * Email: pkendzo@msn.com
 * 2010
 */
package lu.med.praxiswahl.wahlumed.client.rechnungen.op.view;


import java.awt.GridLayout;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;

import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemote;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaskeListener;
import lu.med.praxiswahl.wahlumed.model.beans.OPBillsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.OPBillServiceInt;

public class CustomerDataPanel extends JPanel implements BillItemListener, PatientenSuchmaskeListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 3893537293171184802L;

    private JLabel matrikelLab, customerName, billDate;
    private JTextField matrikelField, customerNameField, billDateField;
    private JDateChooser dateChooser;
    private ContextFactoryRemote remote;
    private OPBillServiceInt opBillService;
    
    /**
     * 
     */
    public CustomerDataPanel()
    {
	super(new GridLayout(2,3));
	this.setBorder(BorderFactory.createTitledBorder("Patient Daten"));
	
	this.matrikelLab = new JLabel("Matrikeln");
	this.matrikelField = new JTextField();
	this.matrikelField.setEditable(false);
	
	this.customerName = new JLabel("Name der Patientin");
	this.customerNameField = new JTextField();
	this.customerNameField.setEditable(false);
	
	this.customerNameField.setCaretPosition(0);
	
	this.billDate = new JLabel("Rechnungsdatum");
	this.billDateField = new JTextField();
	
	this.dateChooser = new JDateChooser(null, null);
	this.dateChooser.getDateEditor().setEnabled(false);
	
	this.add(this.matrikelLab);
	this.add(this.customerName);
	this.add(this.billDate);
	
	this.add(this.matrikelField);
	this.add(this.customerNameField);
	//this.add(this.billDateField);
	this.add(this.dateChooser);
	
	remote = new ContextFactoryRemote(); 
	opBillService = remote.getRemoteOPBillServiceInt();
	
    }
    
    public static void main(String[] arg)
    {
	JFrame frame = new JFrame("Test");
	//frame.setDefaultCloseOperation(System.exit(1));
	
	frame.add(new CustomerDataPanel());
	
	frame.setSize(400, 100);
	frame.setVisible(true);
	
	
    }

    @Override
    public void modelChangeAppend(long billNumber)
    {
	OPBillsTBL bill = opBillService.retrieveOPBillByID(billNumber);
	
	this.matrikelField.setText(""+bill.getCustomer().getMatrikel_nummer());
	
	String firstName = bill.getCustomer().getVorname();
	String lastName = bill.getCustomer().getNachname_1()+" "+bill.getCustomer().getNachname_2();
	String customerName = firstName+" "+lastName;
	
	this.customerNameField.setText(""+customerName);
	
	Calendar date = bill.getErstell_datum();
	Date dateTime = date.getTime();
	
	int day = date.get(Calendar.DAY_OF_MONTH);
	int month = date.get(Calendar.MONTH)+1;
	int year = date.get(Calendar.YEAR);
	
	
	String dayStr = ""+day;
	String monthStr = ""+month;
	
	if(date != null)
	{
	    if(dayStr.length() < 2)
	    {
		dayStr = "0"+dayStr;
	    }
	    if(monthStr.length() < 2)
	    {
		monthStr = "0"+monthStr;
	    }
	   // this.billDateField.setText(dayStr+"."+monthStr+"."+year);
	    this.dateChooser.getDateEditor().setDate(dateTime);
	}
	
	
    }
    
    /*
     * Leer die Anzeige des Panels
     */

    public void clearDisplay()
    {
	this.matrikelField.setText("");
	this.customerNameField.setText("");
	//this.billDateField.setText("");
	this.dateChooser.setDate(null);
    }
    
    @Override
    public void modelChanged(String matricule)
    {
	this.clearDisplay();
    }

    /**
     * @return the matrikelField
     */
    public JTextField getMatrikelField()
    {
        return matrikelField;
    }

    /**
     * @param matrikelField the matrikelField to set
     */
    public void setMatrikelField(JTextField matrikelField)
    {
        this.matrikelField = matrikelField;
    }

    /**
     * @return the customerNameField
     */
    public JTextField getCustomerNameField()
    {
        return customerNameField;
    }

    /**
     * @param customerNameField the customerNameField to set
     */
    public void setCustomerNameField(JTextField customerNameField)
    {
        this.customerNameField = customerNameField;
    }

    /**
     * @return the billDateField
     */
    public JTextField getBillDateField()
    {
        return billDateField;
    }

    /**
     * @param billDateField the billDateField to set
     */
    public void setBillDateField(JTextField billDateField)
    {
        this.billDateField = billDateField;
    }

    /**
     * @return the dateChooser
     */
    public JDateChooser getDateChooser()
    {
        return dateChooser;
    }

    /**
     * @param dateChooser the dateChooser to set
     */
    public void setDateChooser(JDateChooser dateChooser)
    {
        this.dateChooser = dateChooser;
    }

    
}
